using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Bizspoke.BizTalk.Brma;

namespace Bizspoke.BizTalk.HATPlugIn.BizTalk.Extension.HATPlugIn
{
    [DefaultPropertyAttribute("Name")]
    public class ServiceInstance
    {
        private double exitcode;
        private string errorinfo;
        private string host;
        private string state;
        private string instanceid;
        private string activityid;
        private DateTime starttime;
        private DateTime endtime;
        private int duration;
        private string name;
        private string type;
        private string assemblyname;
        private string serviceguid;
        private string serviceclassguid;

        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Error code associated with this instance")]
        public double ExitCode { get { return exitcode; } set { exitcode = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Error description for this service instance")]
        public string ErrorInfo { get { return errorinfo; } set { errorinfo = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Host for this service instance")]
        public string Host { get { return host; } set { host = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Gets one Of the InstanceStatus values currently assigned to this instance")]
        public string State { get { return state; } set { state = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Globally unique identifier (GUID) for this service instance")]
        public string InstanceID { get { return instanceid; } set { instanceid = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("")]
        public string ActivityID { get { return activityid; } set { activityid = value; } }

        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Start time of this service instance")]
        public DateTime StartTime { get { return starttime; } set { starttime = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("End time of this service instance")]
        public DateTime EndTime { get { return endtime; } set { endtime = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("Duration of this service instance")]
        public int Duration { get { return duration; } set { duration = value; } }

        [ReadOnly(true), CategoryAttribute("Service"), DescriptionAttribute("Service name associated with this service instance")]
        public string Name { get { return name; } set { name = value; } }
        [ReadOnly(true), CategoryAttribute("Service"), DescriptionAttribute("Service type associated with this service instance")]
        public string Type { get { return type; } set { type = value; } }
        [ReadOnly(true), CategoryAttribute("Service"), DescriptionAttribute("")]
        public string AssemblyName { get { return assemblyname; } set { assemblyname = value; } }
        [ReadOnly(true), CategoryAttribute("Service"), DescriptionAttribute("")]
        public string ServiceGUID { get { return serviceguid; } set { serviceguid = value; } }
        [ReadOnly(true), CategoryAttribute("Service"), DescriptionAttribute("Service type globally unique identifier (GUID) for this service instance")]
        public string ServiceClassGUID { get { return serviceclassguid; } set { serviceclassguid = value; } }

        public ServiceInstance()
        {
        }

        public ServiceInstance(TrackedInstance trackedInstance)
        {
            this.Name = trackedInstance.TrackingProperties["Service/Name"].Value.ToString();
            this.Type = trackedInstance.TrackingProperties["Service/Type"].Value.ToString();
            this.State = trackedInstance.TrackingProperties["ServiceInstance/State"].Value.ToString();

            DateTime time = DateTime.MinValue;
            DateTime.TryParse(trackedInstance.TrackingProperties["ServiceInstance/StartTime"].Value.ToString(), out time);
            this.StartTime = time;

            time = DateTime.MinValue;
            DateTime.TryParse(trackedInstance.TrackingProperties["ServiceInstance/EndTime"].Value.ToString(), out time);
            this.EndTime = time;

            int duration = 0;
            int.TryParse(trackedInstance.TrackingProperties["ServiceInstance/Duration"].Value.ToString(), out duration);
            this.Duration = duration;

            double exitCode = 0;
            double.TryParse(trackedInstance.TrackingProperties["ServiceInstance/ExitCode"].Value.ToString(), out exitCode);
            this.ExitCode = exitCode;

            this.ErrorInfo = trackedInstance.TrackingProperties["ServiceInstance/ErrorInfo"].Value.ToString();
            this.Host = trackedInstance.TrackingProperties["ServiceInstance/Host"].Value.ToString();
            this.AssemblyName = trackedInstance.TrackingProperties["Service/AssemblyName"].Value.ToString();
            this.InstanceID = trackedInstance.TrackingProperties["ServiceInstance/InstanceID"].Value.ToString();
            this.ActivityID = trackedInstance.TrackingProperties["ServiceInstance/ActivityID"].Value.ToString();
            this.ServiceGUID = trackedInstance.TrackingProperties["Service/ServiceGUID"].Value.ToString();
            this.ServiceClassGUID = trackedInstance.TrackingProperties["Service/ServiceClassGUID"].Value.ToString();

        }
    }

    [DefaultPropertyAttribute("SchemaName")]
    public class MessageInstance
    {
        private string serviceinstanceid;
        private string activityid;
        private string messageinstanceid;
        private string schemaname;
        private string size;
        private int partcount;
        private DateTime timestamp;
        private string direction;
        private string adapter;
        private string url;
        private string decryptioncertificate;
        private string signature;
        private double eventid;
        private string party;
        private string port;
        private MyClass myclass;

        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("serviceinstance_instanceid")]
        public string ServiceInstanceID { get { return serviceinstanceid; } set { serviceinstanceid = value; } }
        [ReadOnly(true), CategoryAttribute("ServiceInstance"), DescriptionAttribute("serviceinstance_activityid")]
        public string ActivityID { get { return activityid; } set { activityid = value; } }

        [ReadOnly(true), CategoryAttribute("MessageInstance"), DescriptionAttribute("messageinstance_instanceid")]
        public string MessageInstanceID { get { return messageinstanceid; } set { messageinstanceid = value; } }
        [ReadOnly(true), CategoryAttribute("MessageInstance"), DescriptionAttribute("messageinstance_schemaname")]
        public string SchemaName { get { return schemaname; } set { schemaname = value; } }
        [ReadOnly(true), CategoryAttribute("MessageInstance"), DescriptionAttribute("messageinstance_size")]
        public string Size { get { return size; } set { size = value; } }
        [ReadOnly(true), CategoryAttribute("MessageInstance"), DescriptionAttribute("messageinstance_partcount")]
        public int PartCount { get { return partcount; } set { partcount = value; } }
        [ReadOnly(true), CategoryAttribute("MessageInstance"), DescriptionAttribute("timestamp")]
        public DateTime Timestamp { get { return timestamp; } set { timestamp = value; } }

        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_direction")]
        public string Direction { get { return direction; } set { direction = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_adapter")]
        public string Adapter { get { return adapter; } set { adapter = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_url")]
        public string URL { get { return url; } set { url = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_decryptioncertificate")]
        public string DecryptionCertificate { get { return decryptioncertificate; } set { decryptioncertificate = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_signature")]
        public string Signature { get { return signature; } set { signature = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_eventid")]
        public double EventID { get { return eventid; } set { eventid = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_party")]
        public string Party { get { return party; } set { party = value; } }
        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_port")]
        public string Port { get { return port; } set { port = value; } }

        [ReadOnly(true), CategoryAttribute("Event"), DescriptionAttribute("event_port")]
        public MyClass MyClass { get { return myclass; } set { myclass = value; } }

        public MessageInstance()
        {
        }

        public MessageInstance(TrackedInstance trackedInstance)
        {
            myclass = new MyClass();
            myclass.Name = "My Name";
            myclass.Kind = "My Kind";

            this.SchemaName = trackedInstance.TrackingProperties["MessageInstance/SchemaName"].Value.ToString();
            this.Direction = trackedInstance.TrackingProperties["Event/Direction"].Value.ToString();

            DateTime time = DateTime.MinValue;
            DateTime.TryParse(trackedInstance.TrackingProperties["Event/Timestamp"].Value.ToString(), out time);
            this.Timestamp = time;

            this.Adapter = trackedInstance.TrackingProperties["Event/Adapter"].Value.ToString();
            this.URL = trackedInstance.TrackingProperties["Event/URL"].Value.ToString();
            this.DecryptionCertificate = trackedInstance.TrackingProperties["Event/DecryptionCertificate"].Value.ToString();
            this.Signature = trackedInstance.TrackingProperties["Event/Signature"].Value.ToString();
            this.ServiceInstanceID = trackedInstance.TrackingProperties["ServiceInstance/InstanceID"].Value.ToString();
            this.ActivityID = trackedInstance.TrackingProperties["ServiceInstance/ActivityID"].Value.ToString();
            this.MessageInstanceID = trackedInstance.TrackingProperties["MessageInstance/InstanceID"].Value.ToString();

            double eventID = 0;
            double.TryParse(trackedInstance.TrackingProperties["Event/EventID"].Value.ToString(), out eventID);
            this.EventID = eventID;

            int partCount = 0;
            int.TryParse(trackedInstance.TrackingProperties["MessageInstance/PartCount"].Value.ToString(), out partCount);
            this.PartCount = partCount;

            this.Size = trackedInstance.TrackingProperties["MessageInstance/Size"].Value.ToString();
            this.Party = trackedInstance.TrackingProperties["Event/Party"].Value.ToString();
            this.Port = trackedInstance.TrackingProperties["Event/Port"].Value.ToString();
        }
    }

    public class MyClass
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string kind;

        public string Kind
        {
            get { return kind; }
            set { kind = value; }
        }

        public MyClass()
        {
        }
    }
}
